package com.qf.controller;

import com.qf.entity.SysUser;
import com.qf.service.IUserService;
import com.qf.service.impl.UserService;
import com.qf.utils.JwtUtils;
import com.qf.utils.RsaUtils;
import com.qf.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;

@RestController
@CrossOrigin
@ResponseBody
@Api(tags = "登录")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService service;

    @PostMapping("/login")
    @ApiOperation("登录")
    public ResultVO login(@RequestBody @ApiParam("用户信息") SysUser user){
        return service.findByPN(user);
    }
    @PostMapping("/check")
    @ApiOperation("登录")
    public ResultVO check(@RequestHeader @ApiParam("用户信息") String token){
        try {
            PublicKey publicKey = RsaUtils.getPublicKey(ResourceUtils.getFile("classpath:rsa.pub").getPath());
            SysUser infoFromToken = (SysUser) JwtUtils.getInfoFromToken(token, publicKey, SysUser.class);
            return new ResultVO(true,"valid token");
        } catch (Exception e) {
            return new ResultVO(false,"invalid token");
        }

    }

}
